package net.phpress.member;

import net.phpress.db.DBConn;

public class MemberDAO extends DBConn{
	public MemberDAO(){
		Connection();
	}

	public int getMaxNum() {
		int result = 0;
		String sql;
		try {
			sql = "select nvl(max(no),0) from member";
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);

			if (rs.next()) {
				result = rs.getInt(1);
			}
			closeConnection();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}
	
	//회원가입
	public int insertData(MemberDTO dto){
		int result = 0;
		String sql;
		try {
			sql = "insert into member (no, id, pw, name, phoneNum, email, mlevel) " +
					"values (?,?,?,?,?,?,?)";
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, dto.getNo());
			pstmt.setString(2, dto.getId());
			pstmt.setString(2, dto.getPw());
			pstmt.setString(2, dto.getName());
			pstmt.setString(2, dto.getPhoneNum());
			pstmt.setString(2, dto.getEmail());
			pstmt.setInt(2, 1);
			result = pstmt.executeUpdate();
			closeConnection();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}
	
	//데이터 읽어오기
	public MemberDTO getReadData(String id){
		MemberDTO dto = null;
		String sql;
		try {
			sql = "select no, id, pw, name, phoneNum, email, mlevel " +
					"from member where id = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, id);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				dto = new MemberDTO();
				dto.setNo(rs.getInt(1));
				dto.setId(rs.getString(2));
				dto.setPw(rs.getString(3));
				dto.setName(rs.getString(4));
				dto.setPhoneNum(rs.getString(5));
				dto.setEmail(rs.getString(6));
				dto.setMlevel(rs.getInt(7));
			}
			closeConnection();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return dto;
	}
}
